<?php
namespace Tbf\Component\FileSystem\Tests;
use Tbf\Component\FileSystem\FileSystemFunction;
class DeleteTest extends FileSystemTestCase
{
    protected $base;
    public function setUp()
    {
        $this->base = __DIR__.'/TestFolder';
        $this->deleteFile();
        $this->createFile();
    }
    public function tearDown()
    {
        $this->deleteFile();
    }
    public function deleteFile()
    {
        FileSystemFunction::delete($this->base);
    }
    public function createFile()
    {
        FileSystemFunction::putContent($this->base.'/f1','');
        FileSystemFunction::mkdir($this->base.'/f2');
        FileSystemFunction::putContent($this->base.'/f3/f4','');
    }
    public function test1()
    {
        //可以删除单个文件
        $this->assertTrue(file_exists($this->base.'/f1'));
        $err = FileSystemFunction::delete($this->base.'/f1');
        $this->assertEquals(null,$err);
        $this->assertTrue(!file_exists($this->base.'/f1'),'可以删除单个文件');

        //可以删除空目录
        $this->assertTrue(file_exists($this->base.'/f2'));
        $err = FileSystemFunction::delete($this->base.'/f2');
        $this->assertEquals(null,$err);
        $this->assertTrue(!file_exists($this->base.'/f2'),'可以删除空目录');

        //递归层数不够
        $this->assertTrue(file_exists($this->base.'/f3'));
        $err = FileSystemFunction::delete($this->base.'/f3',0);
        $this->assertEquals('delete file too deep path:'.$this->base.'/f3/f4',$err);

        //可以删除包含文件的目录
        $this->assertTrue(file_exists($this->base.'/f3/f4'));
        $err = FileSystemFunction::delete($this->base.'/f3');
        $this->assertEquals(null,$err);
        $this->assertTrue(!file_exists($this->base.'/f3'),'可以删除包含文件的目录');

        //删除不存在的文件不报错
        $err = FileSystemFunction::delete($this->base.'/xf7');
        $this->assertEquals(null,$err);

    }
}
